package com.example.bbs.service;

import com.example.bbs.bean.Comment;
import com.example.bbs.bean.Note;
import com.example.bbs.bean.User;
import com.example.bbs.bean.UserExample;
import com.example.bbs.conf.FileConf;
import com.example.bbs.dao.BbstypeMapper;
import com.example.bbs.dao.CommentMapper;
import com.example.bbs.dao.NoteMapper;
import com.example.bbs.dao.UserMapper;
import com.example.bbs.http.bean.HttpSendBasic;
import com.example.bbs.http.bean.user.UserLoginInfo;
import com.example.bbs.http.bean.user.UserShowSimple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private FileConf fileConf;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NoteMapper noteMapper;
    @Autowired
    private BbstypeMapper bbstypeMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private PaserService paserService;

    public HttpSendBasic register(String username, String password) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        if (userMapper.countByExample(userExample) > 0) {
            HttpSendBasic httpSendBasic = new HttpSendBasic();
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("用户名已被注册");
            return httpSendBasic;
        }
        if (password.length() < 6) {
            HttpSendBasic httpSendBasic = new HttpSendBasic();
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("密码过短");
            return httpSendBasic;
        }
        User user = new User();
        user.setHeadimage("/default/headimage.jpg");
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(username);
        userMapper.insertSelective(user);
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        httpSendBasic.setMessage("ok");
        httpSendBasic.setSuccess(1);
        return httpSendBasic;
    }

    public User login(String username, String password) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() != 1)
            return null;
        else
            return users.get(0);
    }

    public String uploadFile(MultipartFile file) throws IOException {
        Date date = new Date();
        String fileName = file.getOriginalFilename();
        File f = new File(fileConf.getFileSave() + "/upload/" + date.getTime() + "/" + fileName);
        f.getParentFile().mkdirs();
        file.transferTo(f);
        return "/file/upload/" + date.getTime() + "/" + fileName;
    }

    public HttpSendBasic sendNote(Integer typeid, String noteName, String note, User user) {
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        if (user == null)
            throw new NullPointerException("user statue can't be null");
        if (bbstypeMapper.selectByPrimaryKey(typeid) == null) {
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("板块不存在");
            return httpSendBasic;
        }
        Note note1 = new Note();
        note1.setTypeid(typeid);
        note1.setCreateby(user.getUserid());
        note1.setNotename(noteName);
        note1.setNote(note);
        note1.setLastedittime(new Date());
        note1.setIsgood(0);
        note1.setTop(0);
        noteMapper.insertSelective(note1);
        httpSendBasic.setMessage("ok");
        httpSendBasic.setSuccess(1);
        return httpSendBasic;
    }

    public HttpSendBasic editNote(Integer noteId, Integer typeid, String noteName, String note, User user) {
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        Note note1 = noteMapper.selectByPrimaryKey(noteId);
        if (note1 == null) {
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("帖子不存在");
            return httpSendBasic;
        }
        if (user == null || !user.getUserid().equals(note1.getCreateby()))
            throw new NullPointerException("user statue can't be null");
        if (typeid != null) {
            if (bbstypeMapper.selectByPrimaryKey(typeid) == null) {
                httpSendBasic.setSuccess(0);
                httpSendBasic.setMessage("板块不存在");
                return httpSendBasic;
            } else
                note1.setTypeid(typeid);
        }
        if (noteName != null)
            note1.setNotename(noteName);
        if (note != null)
            note1.setNote(note);
        note1.setLastedittime(new Date());
        noteMapper.updateByPrimaryKeySelective(note1);
        httpSendBasic.setMessage("ok");
        httpSendBasic.setSuccess(1);
        return httpSendBasic;
    }

    public HttpSendBasic sendComment(Integer noteId, String text, User user) {
        Comment comment = new Comment();
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        if (user == null)
            throw new NullPointerException("user statue can't be null");
        if (noteMapper.selectByPrimaryKey(noteId) == null) {
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("帖子不存在");
            return httpSendBasic;
        }
        comment.setNoteid(noteId);
        comment.setCommenttext(text);
        comment.setCreateby(user.getUserid());
        commentMapper.insertSelective(comment);
        httpSendBasic.setMessage("ok");
        httpSendBasic.setSuccess(1);
        return httpSendBasic;
    }

    public HttpSendBasic editComment(Integer commentId, String text, User user) {
        Comment comment = commentMapper.selectByPrimaryKey(commentId);
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        if (comment == null) {
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("评论不存在");
            return httpSendBasic;
        }
        if (user == null || !user.getUserid().equals(comment.getCreateby()))
            throw new NullPointerException("user statue can't be null");
        if (text != null)
            comment.setCommenttext(text);
        commentMapper.updateByPrimaryKeySelective(comment);
        httpSendBasic.setMessage("ok");
        httpSendBasic.setSuccess(1);
        return httpSendBasic;
    }

    public HttpSendBasic updateUserInfo(Integer userid, String password, String nickname, String headimg) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUseridEqualTo(userid);
        User user = userMapper.selectByExample(userExample).get(0);

        if (password != null && password.length() < 6) {
            HttpSendBasic httpSendBasic = new HttpSendBasic();
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("密码过短");
            return httpSendBasic;
        }
//        User user = new User();
        if (headimg != null)
            user.setHeadimage(headimg);
        if (password != null)
            user.setPassword(password);
        if (nickname != null)
            user.setNickname(nickname);
        userMapper.updateByPrimaryKeySelective(user);
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        httpSendBasic.setMessage("ok");
        httpSendBasic.setSuccess(1);
        return httpSendBasic;
    }

    public UserLoginInfo info(Integer userid) {
        User user = userMapper.selectByPrimaryKey(userid);
        UserLoginInfo userLoginInfo = new UserLoginInfo();
        if (user != null) {
            UserShowSimple userShowSimple = paserService.userTransform(user);
            userLoginInfo.setSuccess(1);
            userLoginInfo.setUser(userShowSimple);
            return userLoginInfo;
        }
        userLoginInfo.setSuccess(0);
        userLoginInfo.setMessage("未找到用户");
        return userLoginInfo;
    }

}
